home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 424_01 / ed_157 / cfg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-11  |  5.5 KB  |  233 lines

  1. /*
  2.  * Copyright (C) 1992 by Rush Record (rhr@clio.rice.edu)
  3.  * 
  4.  * This file is part of ED.
  5.  * 
  6.  * ED is free software; you can redistribute it and/or modify it under the terms
  7.  * of the GNU General Public License as published by the Free Software Foundation.
  8.  * 
  9.  * ED is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  10.  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  11.  * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  12.  * 
  13.  * You should have received a copy of the GNU General Public License along with ED
  14.  * (see the file COPYING).  If not, write to the Free Software Foundation, 675
  15.  * Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17. #include "opsys.h"
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21.  
  22. #include "rec.h"
  23. #include "window.h"
  24. #include "ed_dec.h"    /* global data */
  25. #include "buffer.h"
  26. #include "buf_dec.h"    /* kill buffers */
  27. #include "cmd_enum.h"
  28. #include "key_dec.h"
  29. #include "handy.h"    /* for definition of max() */
  30.  
  31. extern Char *filename();
  32.  
  33. /******************************************************************************\
  34. |Routine: cfg
  35. |Callby: main
  36. |Purpose: Handles creation of new terminal setup files.
  37. |Arguments:
  38. |    termfile is the name of the ED executable, from which the proper directory
  39. |             for .ed files is parsed.
  40. \******************************************************************************/
  41. void cfg(termfile)
  42. Char *termfile;
  43. {
  44.     Int nname,nextra,i,j,k,ln[128];
  45.     Long context;
  46.     Schar c;
  47.     Char *p,*q;
  48.     Schar buf[128][16];
  49.     Char sbuf[128],qbuf[128],dbuf[128],tbuf[128],oldfile[128],newfile[128],curterm[128],finistring[128];
  50.     FILE *fp,*ifp;
  51.     
  52.     BOTROW = NROW;
  53.     marge(1,NROW);
  54.     move(BOTROW,1);
  55.     ers_screen();
  56.     next();
  57. /* parse out the ED executable directory name */
  58.     strcpy(dbuf,termfile);
  59. /* parse out the current terminal name */
  60.     p = filename(dbuf);
  61.     strcpy(curterm,p);
  62.     *p = '\0';
  63.     if((q = (Char *)strchr(curterm,'.')))
  64.         *q = '\0';
  65.     else
  66.         strcpy(curterm,"vt100");
  67. /* list all the existing .ed files */
  68.     strcpy(sbuf,dbuf);
  69.     strcat(sbuf,"*.ed");
  70.     if(!(context = find_files(sbuf)))
  71.     {
  72.         putz("I can't find any .ed files to copy commands from.");
  73.         next();
  74.         putout();
  75.         cleanup(0);
  76.     }
  77.     putz("The following configuration files already exist:");
  78.     next();
  79.     next();
  80.     while(next_file(context,sbuf))
  81.     {
  82.         p = filename(sbuf);
  83.         strcpy(qbuf,p);
  84.         *(strstr(qbuf,".ed")) = '\0';
  85.         putz(qbuf);
  86.         next();
  87.     }
  88.     next();
  89. /* get the user's choice */
  90.     sprintf(qbuf,"Which one do you want to copy terminal control strings from?[%s] ",curterm);
  91.     if(!inquire(qbuf,sbuf,sizeof(sbuf),1))
  92.         strcpy(sbuf,curterm);
  93.     strcpy(oldfile,dbuf);
  94.     strcat(oldfile,sbuf);
  95.     strcat(oldfile,".ed");
  96.     if(!(ifp = fopen(oldfile,"r")))
  97.     {
  98.         next();
  99.         putz("I can't open that .ed file for some reason.");
  100.         next();
  101.         putout();
  102.         perror("cfg");
  103.         cr();
  104.         putout();
  105.         cleanup(0);
  106.     }
  107. /* get the new cfg file name */
  108. redo:
  109.     next();
  110.     if(!inquire("What name do you want for the new configuration file?[abort program] ",qbuf,sizeof(qbuf),1))
  111.     {
  112.         next();
  113.         putout();
  114.         cleanup(0);
  115.     }
  116.     next();
  117.     strcpy(newfile,dbuf);
  118.     strcat(newfile,qbuf);
  119.     strcat(newfile,".ed");
  120.     while(next_file(context,tbuf))
  121.         if(!strcmp(tbuf,newfile))
  122.         {
  123.             if(!inquire("That file already exists, do you want to supersede it?[no] ",tbuf,sizeof(tbuf),1))
  124.             {
  125.                 while(next_file(context,tbuf));
  126.                 goto redo;
  127.             }
  128.             if(!strchr("YyTt1",tbuf[0]))
  129.             {
  130.                 while(next_file(context,tbuf));
  131.                 goto redo;
  132.             }
  133.             break;
  134.         }
  135.     find_files_end(context);
  136. /* create the new cfg file */
  137.     if(!(fp = fopen(newfile,"w")))
  138.     {
  139.         next();
  140.         sprintf(tbuf,"You seem to be unable to create '%s'.",newfile);
  141.         putz(tbuf);
  142.         next();
  143.         putz("Perhaps you need to be the super-user to do this.");
  144.         cr();
  145.         putout();
  146.         cleanup(0);
  147.     }
  148. /* transfer terminal commands from selected file to new file */
  149.     for(i = 0;i < 19;i++)
  150.     {
  151.         fgets(sbuf,sizeof(sbuf),ifp);
  152.         if(!i)    /* initialize the terminal */
  153.             putz(sbuf);
  154.         else if(i == 1)    /* finalize the terminal */
  155.             strcpy(finistring,sbuf);
  156.         fputs(sbuf,fp);
  157.     }
  158.     fclose(ifp);
  159. /* find out how many additional definable keys they want */
  160. redo2:
  161.     if(!(inquire("How many additional keys (besides the keypad) do you want to define?[0] ",sbuf,sizeof(sbuf),1)))
  162.         strcpy(sbuf,"0");
  163.     next();
  164.     if(my_sscanf(sbuf,"%d",&nextra) != 1)
  165.         goto redo2;
  166.     fprintf(fp,"%d\n",nname = 44 + nextra);
  167.     putz("If you make a mistake after you've started entering a key,");
  168.     next();
  169.     putz("press control-C and hit RETURN.");
  170.     next();
  171.     next();
  172.     putz("If you press control-C and hit RETURN before you've pressed any other keys,");
  173.     next();
  174.     putz("the program will abort.");
  175.     next();
  176.     next();
  177. /* get the key strings */
  178.     for(i = 0;i < nname;i++)
  179.     {
  180. redo3:
  181.         putz("Press the '");
  182.         putz(keyname[127 - i]);
  183.         putz("' key, and then hit RETURN: ");
  184.         putout();
  185.         for(j = 0;j < sizeof(buf[i]);j++)
  186.         {
  187.             ttyget(&c);
  188.             if(c == 13)
  189.                 break;
  190.             buf[i][j] = c;
  191.         }
  192.         next();
  193.         if(!(ln[i] = j))
  194.         {
  195.             putz("That key didn't generate any characters, use some other key.");
  196.             next();
  197.             goto redo3;
  198.         }
  199.         else if(ln[i] == 1 && buf[i][0] == 3)
  200.         {
  201.             putz("Program aborted.");
  202.             cr();
  203.             putz(finistring);
  204.             putout();
  205.             cleanup(0);
  206.         }
  207.         else if(buf[i][j - 1] == 3)
  208.             goto redo3;
  209.     }
  210. /* put the strings in the cfg file */
  211.     for(i = 0;i < nname;i++)
  212.     {
  213.         for(k = 0;k < ln[i];k++)
  214.         {
  215.             if(((buf[i][k] >= 0 && buf[i][k] < ' ') || buf[i][k] < 0) && buf[i][k] != 27)
  216.                 fprintf(fp,"%c%03d",29,buf[i][k] & 0xff);
  217.             else
  218.                 fprintf(fp,"%c",buf[i][k]);
  219.         }
  220.         fprintf(fp,"\n");
  221.     }
  222.     fclose(fp);
  223.     next();
  224.     putz("The new configuration file (");
  225.     putz(newfile);
  226.     putz(") is complete.");
  227.     cr();
  228.     putz(finistring);
  229.     putout();
  230.     cleanup(0);
  231. }
  232.  
  233.